﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using System.ComponentModel;
using System.Globalization;

namespace TimeReporter
{
    public class ProjectObject :INotifyPropertyChanged
    {
        private int _projectID;
        private string _projectName;
        private string _projectCode;
        private myBindingListView<TaskObject> _theTasks = new myBindingListView<TaskObject>();
        private Dictionary<DayOfWeek, float> _timePerDay = new Dictionary<DayOfWeek,float>();

        public event PropertyChangedEventHandler PropertyChanged;
        
        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public ProjectObject()
        {
            
        }

        public ProjectObject(FileInfo fi)
        {
            _projectName = ProjectObject.FileInfoToProjectName(fi);
            _projectCode = ProjectObject.FileInfoToProjectCode(fi);
        }

        public ProjectObject(FileInfo fi, int ProjectID)
        {
            _projectName = ProjectObject.FileInfoToProjectName(fi);
            _projectCode = ProjectObject.FileInfoToProjectCode(fi);
        }

        public int ProjectID
        {
            get { return _projectID; }
            set { _projectID = value; }
        }
        
        public string ProjectCode
        {
            get { return _projectCode; }
        }

        public string ProjectName
        {
            get { return _projectName; }
            // set { _projectName = value; }
        }

        public myBindingListView<TaskObject> theTasks
        {
            get { return _theTasks; }
            set { _theTasks = value; }
        }

        public void SummarizeWeek(bool useLastWeek)
        {
            _timePerDay.Clear();
            
            DateTime[] theRange = getWeekRange(useLastWeek);
            Console.WriteLine("Project id: {0}", _projectCode);
            //Console.WriteLine("Start date is:" + theRange[0].ToString());
            //Console.WriteLine("End date is:" + theRange[1].ToString());

            var task =
                from t in _theTasks
                where t.StartDateTime >= theRange[0] && t.StartDateTime <= theRange[1]
                select t;

            var task2 =
                from t in task
                group t by new {t.StartDateTime } into g
                select new {g.Key.StartDateTime, timePerDay = g.Sum(t => t.TimeSpent) };

            
            foreach (var z in task2)
            {
                _timePerDay.Add(z.StartDateTime.DayOfWeek, z.timePerDay);
                Console.WriteLine("Startdate: {0}, taskID: {1}, totaltime:{2}", z.StartDateTime, "None", z.timePerDay);
            }

            NotifyPropertyChanged("timePerDay");

        }

        private static string FileInfoToProjectName(FileInfo fi)
        {
            char[] charArray = new char[] { '_' };
            string[] tmp = fi.Name.Split(charArray);
            int pos = tmp[0].IndexOf(" ");

            return tmp[0].Substring(pos);
        }
        private static string FileInfoToProjectCode(FileInfo fi)
        {
            char[] charArray = new char[] { ' ' };
            string[] tmp = fi.Name.Split(charArray);

            return tmp[0].Trim();
        }

        private DateTime[] getWeekRange(bool UsePreviousWeek)
        {
            DayOfWeek today;
            DateTime[] range = new DateTime[2];
            Calendar theCal = CultureInfo.InvariantCulture.Calendar;
            DateTime now = DateTime.Today;

            CultureInfo myCu = new CultureInfo("se-SE");
            CalendarWeekRule firstweek = myCu.DateTimeFormat.CalendarWeekRule;
            DayOfWeek firstday = myCu.DateTimeFormat.FirstDayOfWeek;

            int thisweek = theCal.GetWeekOfYear(now, firstweek, firstday);

            if (UsePreviousWeek)
            {
                range[0] = FirstDayInWeek.GetFirstDateOfWeek(theCal.AddWeeks(now, -1));
                range[1] = range[0].AddDays(6);
            }
            else
            {
                today = DateTime.Today.DayOfWeek;
                range[0] = FirstDayInWeek.GetFirstDateOfWeek(DateTime.Today);
                range[1] = range[0].AddDays(6);
            }

            return range;

        }
    }
}
